﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;

namespace Besture.UI
{
    public partial class UserBaseListSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
			//默认加载当天日期
			txtBirthday.Text = DateTime.Now.ToString("yyyy-MM-dd");
			//默认加载当天日期
			txtStatusTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
			//默认加载当天日期
			txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
			//默认加载当天日期
			txtUpdateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
			
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new UserBaseDac();
            var model = dac.GetModel(id);
			if(model==null) return;

            txtLoginName.Text = model.LoginName.ToString();
			txtNickName.Text = model.NickName.ToString();
			txtLoginPwd.Text = model.LoginPwd.ToString();
			txtUserType.Text = model.UserType.ToString();
			txtPhone.Text = model.Phone.ToString();
			txtEmail.Text = model.Email.ToString();
			txtRealName.Text = model.RealName.ToString();
			txtSex.Text = model.Sex.ToString();
			txtUserImage.Text = model.UserImage.ToString();
			txtBirthday.Text =  Convert.ToDateTime(model.Birthday).ToString("yyyy-MM-dd");
			txtAreaCode.Text = model.AreaCode.ToString();
			txtTel.Text = model.Tel.ToString();
			txtTelExt.Text = model.TelExt.ToString();
			txtProvinceID.Text = model.ProvinceID.ToString();
			txtCityID.Text = model.CityID.ToString();
			txtCompanyName.Text = model.CompanyName.ToString();
			txtRepresentative.Text = model.Representative.ToString();
			txtAddress.Text = model.Address.ToString();
			txtUserRating.Text = model.UserRating.ToString();
			txtRatingIntegration.Text = model.RatingIntegration.ToString();
			txtUserPoints.Text = model.UserPoints.ToString();
			txtReviewerID.Text = model.ReviewerID.ToString();
			txtCheckStatus.Text = model.CheckStatus.ToString();
			txtStatusTime.Text =  Convert.ToDateTime(model.StatusTime).ToString("yyyy-MM-dd");
			txtSortID.SortValue = model.SortID != 0 ? model.SortID : 0;
			txtIsDeleted.Text = model.IsDeleted.ToString();
			txtCreated.Text = model.Created.ToString();
			txtCreateTime.Text =  Convert.ToDateTime(model.CreateTime).ToString("yyyy-MM-dd");
			txtUpdated.Text = model.Updated.ToString();
			txtUpdateTime.Text =  Convert.ToDateTime(model.UpdateTime).ToString("yyyy-MM-dd");

			
        }
        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new UserBaseDac();
            UserBaseModel model=null;
            var baseDac = new BaseDac<UserBaseModel>();
            //添加
            if (!EditPage)
            {
                model = new UserBaseModel();
                model.LoginName = txtLoginName.Text.Trim();
				model.NickName = txtNickName.Text.Trim();
				model.LoginPwd = txtLoginPwd.Text.Trim();
				model.UserType = txtUserType.Text.Trim();
				model.Phone = txtPhone.Text.Trim();
				model.Email = txtEmail.Text.Trim();
				model.RealName = txtRealName.Text.Trim();
				model.Sex = txtSex.Text.Trim();
				model.UserImage = txtUserImage.Text.Trim();
				model.Birthday = txtBirthday.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtBirthday.Text.Trim());
				model.AreaCode = txtAreaCode.Text.Trim();
				model.Tel = txtTel.Text.Trim();
				model.TelExt = txtTelExt.Text.Trim();
				model.ProvinceID = txtProvinceID.Text.Trim();
				model.CityID = txtCityID.Text.Trim();
				model.CompanyName = txtCompanyName.Text.Trim();
				model.Representative = txtRepresentative.Text.Trim();
				model.Address = txtAddress.Text.Trim();
				model.UserRating = txtUserRating.Text.Trim();
				model.RatingIntegration = txtRatingIntegration.Text.Trim();
				model.UserPoints = txtUserPoints.Text.Trim();
				model.ReviewerID = txtReviewerID.Text.Trim();
				model.CheckStatus = txtCheckStatus.Text.Trim();
				model.StatusTime = txtStatusTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtStatusTime.Text.Trim());
				model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
				model.IsDeleted = txtIsDeleted.Text.Trim();
				model.Created = txtCreated.Text.Trim();
				model.CreateTime = txtCreateTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtCreateTime.Text.Trim());
				model.Updated = txtUpdated.Text.Trim();
				model.UpdateTime = txtUpdateTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtUpdateTime.Text.Trim());

                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
				

                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.LoginName = txtLoginName.Text.Trim();
				model.NickName = txtNickName.Text.Trim();
				model.LoginPwd = txtLoginPwd.Text.Trim();
				model.UserType = txtUserType.Text.Trim();
				model.Phone = txtPhone.Text.Trim();
				model.Email = txtEmail.Text.Trim();
				model.RealName = txtRealName.Text.Trim();
				model.Sex = txtSex.Text.Trim();
				model.UserImage = txtUserImage.Text.Trim();
				model.Birthday = txtBirthday.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtBirthday.Text.Trim());
				model.AreaCode = txtAreaCode.Text.Trim();
				model.Tel = txtTel.Text.Trim();
				model.TelExt = txtTelExt.Text.Trim();
				model.ProvinceID = txtProvinceID.Text.Trim();
				model.CityID = txtCityID.Text.Trim();
				model.CompanyName = txtCompanyName.Text.Trim();
				model.Representative = txtRepresentative.Text.Trim();
				model.Address = txtAddress.Text.Trim();
				model.UserRating = txtUserRating.Text.Trim();
				model.RatingIntegration = txtRatingIntegration.Text.Trim();
				model.UserPoints = txtUserPoints.Text.Trim();
				model.ReviewerID = txtReviewerID.Text.Trim();
				model.CheckStatus = txtCheckStatus.Text.Trim();
				model.StatusTime = txtStatusTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtStatusTime.Text.Trim());
				model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
				model.IsDeleted = txtIsDeleted.Text.Trim();
				model.Created = txtCreated.Text.Trim();
				model.CreateTime = txtCreateTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtCreateTime.Text.Trim());
				model.Updated = txtUpdated.Text.Trim();
				model.UpdateTime = txtUpdateTime.Text.Trim() == "" ? Convert.ToDateTime("0001/01/01") : Convert.ToDateTime(txtUpdateTime.Text.Trim());

                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
				

                dac.Update(model);
            }

            FunHelper.Alert("保存成功", ListUrl());
        }
        #endregion
    }
}